package cn.iocoder.yudao.module.iot.controller.admin.deviceeventslog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.iot.controller.admin.deviceeventslog.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceeventslog.DeviceEventsLogDO;
import cn.iocoder.yudao.module.iot.convert.deviceeventslog.DeviceEventsLogConvert;
import cn.iocoder.yudao.module.iot.service.deviceeventslog.DeviceEventsLogService;

@Tag(name = "管理后台 - 设备事件日志")
@RestController
@RequestMapping("/iot/deviceEventslog")
@Validated
public class DeviceEventsLogController {

    @Resource
    private DeviceEventsLogService deviceEventsLogService;

    @PostMapping("/create")
    @Operation(summary = "创建设备事件日志")
    @PreAuthorize("@ss.hasPermission('iot:device-events-log:create')")
    public CommonResult<Long> createDeviceEventsLog(@Valid @RequestBody DeviceEventsLogCreateReqVO createReqVO) {
        return success(deviceEventsLogService.createDeviceEventsLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备事件日志")
    @PreAuthorize("@ss.hasPermission('iot:device-events-log:update')")
    public CommonResult<Boolean> updateDeviceEventsLog(@Valid @RequestBody DeviceEventsLogUpdateReqVO updateReqVO) {
        deviceEventsLogService.updateDeviceEventsLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备事件日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-events-log:delete')")
    public CommonResult<Boolean> deleteDeviceEventsLog(@RequestParam("id") Long id) {
        deviceEventsLogService.deleteDeviceEventsLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备事件日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-events-log:query')")
    public CommonResult<DeviceEventsLogRespVO> getDeviceEventsLog(@RequestParam("id") Long id) {
        DeviceEventsLogDO deviceEventsLog = deviceEventsLogService.getDeviceEventsLog(id);
        return success(DeviceEventsLogConvert.INSTANCE.convert(deviceEventsLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备事件日志列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('iot:device-events-log:query')")
    public CommonResult<List<DeviceEventsLogRespVO>> getDeviceEventsLogList(@RequestParam("ids") Collection<Long> ids) {
        List<DeviceEventsLogDO> list = deviceEventsLogService.getDeviceEventsLogList(ids);
        return success(DeviceEventsLogConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @Operation(summary = "获得设备事件日志分页")
    //@PreAuthorize("@ss.hasPermission('iot:device-events-log:query')")
    public CommonResult<PageResult<DeviceEventsLogRespVO>> getDeviceEventsLogPage(@Valid DeviceEventsLogPageReqVO pageVO) {
        PageResult<DeviceEventsLogDO> pageResult = deviceEventsLogService.getDeviceEventsLogPage(pageVO);
        PageResult<DeviceEventsLogRespVO> result = DeviceEventsLogConvert.INSTANCE.convertPage(pageResult);
        List<DeviceEventsLogRespVO> list = result.getList();
        if(list!=null&&list.size()>0){
            for (DeviceEventsLogRespVO vo:list) {
                vo.setActionDesc(vo.getAction());
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备事件日志 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-events-log:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceEventsLogExcel(@Valid DeviceEventsLogExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DeviceEventsLogDO> list = deviceEventsLogService.getDeviceEventsLogList(exportReqVO);
        // 导出 Excel
        List<DeviceEventsLogExcelVO> datas = DeviceEventsLogConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备事件日志.xls", "数据", DeviceEventsLogExcelVO.class, datas);
    }

}
